//
//  CommentViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/12.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "CommentViewController.h"
#import "CommentTableViewCell.h"
#import "CommentModel.h"

@interface CommentViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) NSMutableArray *dataArray1;
@property (nonatomic, strong) NSMutableArray *dataArray2;
@property (nonatomic, strong) NSMutableArray *dataArray3;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@property (nonatomic, strong) UIButton *btn3;
@property (nonatomic, strong) UIButton *btn4;

@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = tableViewBgColor;
    [self loadBaseTitleLable:@"商品评价"];
    
    self.currentPage = 1;
    self.pageSize = 20;
    self.selectIndex = 1;
    
    [self setUpUI];
    
    [self getPingJiaNum];
    [self getGoodsListData];
}
#pragma mark - 评价数量
- (void)getPingJiaNum
{
    if (self.goodsId) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/shop/shop.ashx?action=shopdata&companyid=%@&id=%@",kServerURL,companyId,self.goodsId] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            
            if ([responseObject[@"status"] intValue] == 1) {
                [self.btn1 setTitle:[NSString stringWithFormat:@"全部评价\n%@",responseObject[@"evaluate"]] forState:(UIControlStateNormal)];
                [self.btn2 setTitle:[NSString stringWithFormat:@"好评\n%@",responseObject[@"evaluate_good"]] forState:(UIControlStateNormal)];
                [self.btn3 setTitle:[NSString stringWithFormat:@"中评\n%@",responseObject[@"evaluate_ordinary"]] forState:(UIControlStateNormal)];
                [self.btn4 setTitle:[NSString stringWithFormat:@"差评\n%@",responseObject[@"evaluate_bad"]] forState:(UIControlStateNormal)];
            } else {
                [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
            }
            
        } failure:^(NSError *error) {
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
            
        }];
    }
}
#pragma mark - 评价列表
- (void)getGoodsListData
{
    NSString *level;
    if (self.selectIndex == 1) {
        // 全部
        level = @"";
    } else if (self.selectIndex == 2) {
        // 好评
        level = @"1";
    } else if (self.selectIndex == 3) {
        // 中评
        level = @"2";
    } else if (self.selectIndex == 4) {
        // 差评
        level = @"3";
    }
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/common/common.ashx?action=commodityevaluation&companyid=%@&id=%@&pagesize=%@&pageindex=1&level=%@",kServerURL,companyId,self.goodsId,[NSString stringWithFormat:@"%ld",(long)self.pageSize],level] parameters:nil success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:self.view];
        self.dataArray = [NSMutableArray array];
        self.currentPage = 1;
        if ([responseObject[@"status"] intValue] == 1) {
            self.dataArray = [CommentModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        }
        NSInteger i = [responseObject[@"count"] intValue]/self.pageSize;
        NSInteger j = [responseObject[@"count"] intValue]%self.pageSize;
        NSInteger n = 0;
        if (j > 0) {
            n = i+1;
        } else {
            n = i;
        }
        if (n<2) {
            self.tableView.mj_footer.hidden = YES;
        } else {
            self.tableView.mj_footer.hidden = NO;
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)getGoodsListDataMore
{
    NSString *level;
    if (self.selectIndex == 1) {
        // 全部
        level = @"";
    } else if (self.selectIndex == 2) {
        // 好评
        level = @"1";
    } else if (self.selectIndex == 3) {
        // 中评
        level = @"2";
    } else if (self.selectIndex == 4) {
        // 差评
        level = @"3";
    }
    self.currentPage++;
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/common/common.ashx?action=commodityevaluation&companyid=%@&id=%@&pagesize=%@&pageindex=%@&level=%@",kServerURL,companyId,self.goodsId,[NSString stringWithFormat:@"%ld",(long)self.pageSize],[NSString stringWithFormat:@"%ld",(long)self.currentPage],level] parameters:nil success:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:self.view];
        if ([responseObject[@"status"] intValue] == 1) {
            
            NSInteger i = [responseObject[@"count"] intValue]/self.pageSize;
            NSInteger j = [responseObject[@"count"] intValue]%self.pageSize;
            NSInteger n = 0;
            if (j > 0) {
                n = i+1;
            } else {
                n = i;
            }
            if (self.currentPage > n) {
                self.currentPage--;
                self.tableView.mj_footer.hidden = YES;
            } else {
                self.tableView.mj_footer.hidden = NO;
                self.dataArray = (NSMutableArray *)[self.dataArray arrayByAddingObjectsFromArray:[CommentModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
                [self.tableView reloadData];
            }
        } else {
            self.currentPage--;
            self.tableView.mj_footer.hidden = YES;
        }
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (void)setUpUI
{
    self.btnArray = [NSMutableArray array];
    
    UIButton *btn1 = [UIButton buttonWithTitle:@"全部评价\n3" fontSize:14 titleColor:navBarColor background:UIColorWhite cornerRadius:0];
    btn1.titleLabel.numberOfLines = 0;
    btn1.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:btn1];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view).offset(SafeAreaTopHeight);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(100)));
    }];
    btn1.tag = 1;
    
    UIButton *btn2 = [UIButton buttonWithTitle:@"好评\n2" fontSize:14 titleColor:UIColorFromRGB(0x333333) background:UIColorWhite cornerRadius:0];
    btn2.titleLabel.numberOfLines = 0;
    btn2.titleLabel.textAlignment
    
    = NSTextAlignmentCenter;
    [self.view addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn1.mas_right);
        make.top.equalTo(self.view).offset(SafeAreaTopHeight);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(100)));
    }];
    btn2.tag = 2;
    
    UIButton *btn3 = [UIButton buttonWithTitle:@"中评\n1" fontSize:14 titleColor:UIColorFromRGB(0x333333) background:UIColorWhite cornerRadius:0];
    btn3.titleLabel.numberOfLines = 0;
    btn3.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:btn3];
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn2.mas_right);
        make.top.equalTo(self.view).offset(SafeAreaTopHeight);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(100)));
    }];
    btn3.tag = 3;
    
    UIButton *btn4 = [UIButton buttonWithTitle:@"差评\n0" fontSize:14 titleColor:UIColorFromRGB(0x333333) background:UIColorWhite cornerRadius:0];
    btn4.titleLabel.numberOfLines = 0;
    btn4.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:btn4];
    [btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn3.mas_right);
        make.top.equalTo(self.view).offset(SafeAreaTopHeight);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(100)));
    }];
    btn4.tag = 4;
    self.btn1 = btn1;
    self.btn2 = btn2;
    self.btn3 = btn3;
    self.btn4 = btn4;
    [self.btnArray addObject:btn1];
    [self.btnArray addObject:btn2];
    [self.btnArray addObject:btn3];
    [self.btnArray addObject:btn4];
    [btn1 addTarget:self action:@selector(actionTag:) forControlEvents:(UIControlEventTouchUpInside)];
    [btn2 addTarget:self action:@selector(actionTag:) forControlEvents:(UIControlEventTouchUpInside)];
    [btn3 addTarget:self action:@selector(actionTag:) forControlEvents:(UIControlEventTouchUpInside)];
    [btn4 addTarget:self action:@selector(actionTag:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.tableView];
}
- (void)actionTag:(UIButton *)btn
{
    self.selectIndex = btn.tag;
    for (UIButton *btn1 in self.btnArray) {
        if (btn.tag == btn1.tag) {
            [btn1 setTitleColor:navBarColor forState:(UIControlStateNormal)];
        } else {
            [btn1 setTitleColor:UIColorFromRGB(0x333333) forState:(UIControlStateNormal)];
        }
    }
    [self getGoodsListData];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentTableViewCell *cell = [[CommentTableViewCell alloc] init];
    // wuxinghui wuxingjin
    if (_dataArray.count != 0) {
        CommentModel *model = _dataArray[indexPath.section];
        cell.contentLB.text = model.commentContent;
        cell.imgArray = [model.commentImages componentsSeparatedByString:@"|"];
        if ([model.commentLevel intValue] == 0) {
            [cell.star.star1 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star2 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star3 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
        } else if ([model.commentLevel intValue] == 1) {
            [cell.star.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star2 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star3 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
        } else if ([model.commentLevel intValue] == 2) {
            [cell.star.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star3 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
        } else if ([model.commentLevel intValue] == 3) {
            [cell.star.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star3 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
            [cell.star.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
        } else if ([model.commentLevel intValue] == 4) {
            [cell.star.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star3 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star4 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
        } else if ([model.commentLevel intValue] == 5) {
            [cell.star.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star3 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star4 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
            [cell.star.star5 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
        }
    }
    cell.selectionStyle = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArray.count != 0) {
        CommentModel *model = _dataArray[indexPath.section];
        NSArray *array = [model.commentImages componentsSeparatedByString:@"|"];
        if (array.count < 4) {
            return rateHeight(180)+rateHeight(220);
        } else if (array.count > 3 && array.count < 7) {
            return rateHeight(180)+rateHeight(440);
        } else if (array.count > 6) {
            return rateHeight(180)+rateHeight(660);
        } else {
            return 0.01;
        }
    } else {
        return 0.01;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return rateHeight(140);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return rateHeight(100);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = UIColorWhite;
    
    UIImageView *headImg = [[UIImageView alloc] init];
    headImg.backgroundColor = globalGray;
    headImg.layer.masksToBounds = YES;
    headImg.layer.cornerRadius = rateHeight(40);
    [bgV addSubview:headImg];
    [headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgV);
        make.left.equalTo(bgV).offset(rateWidth(20));
        make.size.mas_offset(CGSizeMake(rateHeight(80), rateHeight(80)));
    }];
    UILabel *nameLB = [UILabel labelWithText:@"" textColor:UIColorFromRGB(0x333333) fontSize:15];
    [nameLB sizeToFit];
    [bgV addSubview:nameLB];
    [nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgV);
        make.left.equalTo(headImg.mas_right).offset(rateWidth(10));
    }];
    UILabel *gradeLB = [UILabel labelWithText:@"普通会员" textColor:UIColorFromRGB(0x333333) fontSize:14];
    gradeLB.layer.borderColor = UIColorWithRGB(149, 226, 177).CGColor;
    gradeLB.layer.borderWidth = 1;
    gradeLB.textAlignment = NSTextAlignmentCenter;
    [bgV addSubview:gradeLB];
    [gradeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLB.mas_right).offset(rateWidth(20));
        make.centerY.equalTo(bgV);
        make.size.mas_offset(CGSizeMake(rateWidth(140), rateHeight(50)));
    }];
    UILabel *timeLB = [UILabel labelWithText:@"" textColor:UIColorFromRGB(0x333333) fontSize:12];
    [timeLB sizeToFit];
    [bgV addSubview:timeLB];
    [timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgV).offset(-rateWidth(20));
        make.centerY.equalTo(bgV);
    }];
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = globalGray;
    [bgV addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgV);
        make.left.equalTo(bgV).offset(rateWidth(20));
        make.right.equalTo(bgV).offset(-rateWidth(20));
        make.height.equalTo(@(rateHeight(2)));
    }];
    // 赋值
    if (_dataArray.count != 0) {
        CommentModel *model = _dataArray[section];
        [headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",model.memberFace]]];
        nameLB.text = model.commentName;
        timeLB.text = model.commentTime;
        if ([model.memberLevel length] != 0) {
            gradeLB.text = model.memberLevel;
        }
    }
    return bgV;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = tableViewBgColor;
    UIView *whiteV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, rateHeight(80))];
    whiteV.backgroundColor = UIColorWhite;
    [bgV addSubview:whiteV];
    UILabel *timeLB = [UILabel labelWithText:@"购买时间：2018-07-07 12:10" textColor:UIColorFromRGB(0x666666) fontSize:13];
    [timeLB sizeToFit];
    [whiteV addSubview:timeLB];
    [timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(whiteV).offset(rateWidth(20));
        make.centerY.equalTo(whiteV);
    }];
    
    return bgV;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight+rateHeight(100)+rateHeight(20), kScreenWidth, kScreenHeight-SafeAreaTopHeight-rateHeight(120)) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = tableViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        WeakSelf
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getGoodsListData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getGoodsListDataMore];
        }];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
